package com.yaolonga.itfc.manager.linux;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yaolong
 * @CreateTime: 2024-11-25  11:37
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
public class WIfiScanHelper {
    public static Map<String, Map<String, Object>> parseWifiScanInfo(InputStream inputStream) {
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line;
        Map<String, Map<String, Object>> bssids = new HashMap<>();
        try {
            // 是否忽略行
            boolean isHeader = true;
            HashMap<String, Object> bssid = null;
            Map<String, Map<String, Integer>> readIndexMap = new HashMap<>();
            while ((line = bufferedReader.readLine()) != null) {
//                System.out.println(line);
                if (line.trim().isEmpty()) {
                    continue;
                }
                line = line.trim();
                if (isHeader) {
                    readIndexMap = getFieldIndexes(line);
                    isHeader = false;
                    continue;
                }
                Map<String, Object> wifiInfoMap = new HashMap<>();
                wifiInfoMap.put("NAME", getFieldValue("NAME", line, readIndexMap));
                wifiInfoMap.put("SSID", getFieldValue("SSID", line, readIndexMap));
                wifiInfoMap.put("SSID-HEX", getFieldValue("SSID-HEX", line, readIndexMap));
                wifiInfoMap.put("BSSID", getFieldValue("BSSID", line, readIndexMap));
                wifiInfoMap.put("MODE", getFieldValue("MODE", line, readIndexMap));
                wifiInfoMap.put("CHAN", getFieldValue("CHAN", line, readIndexMap));
                wifiInfoMap.put("FREQ", getFieldValue("FREQ", line, readIndexMap));
                wifiInfoMap.put("RATE", getFieldValue("RATE", line, readIndexMap));
                wifiInfoMap.put("SIGNAL", getFieldValue("SIGNAL", line, readIndexMap));
                wifiInfoMap.put("BARS", getFieldValue("BARS", line, readIndexMap));
                wifiInfoMap.put("SECURITY", getFieldValue("SECURITY", line, readIndexMap));
                wifiInfoMap.put("WPA-FLAGS", getFieldValue("WPA-FLAGS", line, readIndexMap));
                wifiInfoMap.put("RSN-FLAGS", getFieldValue("RSN-FLAGS", line, readIndexMap));
                wifiInfoMap.put("DEVICE", getFieldValue("DEVICE", line, readIndexMap));
                wifiInfoMap.put("ACTIVE", getFieldValue("ACTIVE", line, readIndexMap));
                wifiInfoMap.put("IN-USE", getFieldValue("IN-USE", line, readIndexMap));
                wifiInfoMap.put("DBUS-PATH", getFieldValue("DBUS-PATH", line, readIndexMap));
                bssids.put((String) wifiInfoMap.get("BSSID"), wifiInfoMap);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return bssids;
    }

    private static Map<String, Map<String, Integer>> getFieldIndexes(String input) {
        input = input.trim();
        Map<String, Map<String, Integer>> result = new HashMap<>();
        int currentIndex = 0;
        int currentStartIndex = 0;
        int currentEndIndex = 0;
        String preField = "";
        while (currentIndex < input.length()) {
            if (isWordStartBoundary(input, currentIndex)) {
                currentStartIndex = currentIndex;
            } else if (isWordEndBoundary(input, currentIndex)) {
                currentEndIndex = currentIndex;
                String field = input.substring(currentStartIndex, currentEndIndex);
                if (!preField.isEmpty()) {
                    result.get(preField).put("end", currentStartIndex - 2);
                }

                Map<String, Integer> map = new HashMap<>();
                map.put("start", currentStartIndex);
                result.put(field, map);
                preField = field;
                WIfiScanHelper.log.debug(field + ":" + currentIndex + "  start: " + currentStartIndex + " end:" + (currentStartIndex - 2));
            }
            currentIndex++;
        }

        String field = input.substring(currentStartIndex).trim();
        Map<String, Integer> map = new HashMap<>();
        map.put("start", currentStartIndex);
        map.put("end", -1);
        result.put(field, map);
        return result;
    }

    private static boolean isWordEndBoundary(String s, int index) {
        if (index == s.length() - 1) return true;
        char currentChar = s.charAt(index);
        char prevChar = s.charAt(index - 1);
        return currentChar == ' ' && prevChar != ' ';
    }

    private static boolean isWordStartBoundary(String s, int index) {
        if (index == 0) return true;
        char currentChar = s.charAt(index);
        char prevChar = s.charAt(index - 1);
        return currentChar != ' ' && prevChar == ' ';
    }

    private static String getFieldValue(String field, String input, Map<String, Map<String, Integer>> readIndexMap) {
        Map<String, Integer> scope = readIndexMap.get(field);
        if (scope != null) {
            Integer start = scope.get("start");
            Integer end = scope.get("end");
            if (end == -1) {
                return input.substring(start).trim();
            }
            return input.substring(start, end).trim();
        }
        return null;
    }


}
